<?php echo $this->element('paginator_ajax_settings'); ?>
<h1>Xem tất cả bookings</h1>

<div id="filter">
    <div class="time">
        Thời gian:
        <?php echo $this->Form->input('Booking.month', array('type' => 'select', 'options' => $month, 'class' => 'dropdown', 'label' => false, 'div' => false)); ?>

        <?php echo $this->Form->input('Booking.year', array('type' => 'select', 'options' => $year, 'class' => 'dropdown', 'label' => false, 'div' => false)); ?>
    </div>

    <div class="search">
        Tìm kiếm:
        <?php echo $this->Form->input('Booking.query', array('type' => 'text', 'value' => isset($query) ? htmlspecialchars_decode($query) : '', 'class' => 'textfield', 'label' => false, 'div' => false)); ?> <span class="light">Tên, Mã booking</span>
    </div>

    <br class="clr" />
</div>

<script type="text/javascript">
$(document).ready(function (){
    var timerid;
    <?php if (isset($query)): ?>
    $('#BookingQuery').focus();
    <?php endif; ?>
    $('#BookingQuery').keyup(function (e){
        if (e.keyCode == 13)
        {
            search($('#BookingQuery').val(), $('#BookingMonth').val(), $('#BookingYear').val());
        }
    });
    $('#BookingQuery').click(function (e){
        $('#BookingQuery').select();
    });
    $('#BookingMonth').change(function (e){
        search($('#BookingQuery').val(), $('#BookingMonth').val(), $('#BookingYear').val());
    });
    $('#BookingYear').change(function (e){
        search($('#BookingQuery').val(), $('#BookingMonth').val(), $('#BookingYear').val());
    });
    function search(query, month, year)
    {
        if (month == 'undefined' || month == null)
        {
            month = $('#BookingMonth').val();
        }
        if (year == 'undefined' || year == null)
        {
            year = $('#BookingYear').val();
        }
        //query = $('#CustomerQuery').val();
        clearTimeout(timerid);
        timerid = setTimeout(function (){
            $.ajax({
                async: true,
                type: "POST",
                url: "bookings/index/query:" + query + "/month:" + month + "/year:" + year,
                //data: "",
                success: function(msg){
                    $('#main').html(msg);
                },
                beforeSend: function (XMLHttpRequest){
                    $("#busy-indicator").fadeIn();
                },
                complete: function (XMLHttpRequest, textStatus){
                    $("#busy-indicator").fadeOut();
                }
            });
        }, 600);
    }
    var availableTags = [
        <?php
        if (!empty($all_bookings))
        {
            $count = 0;
            $bookings_count = sizeof($all_bookings);
            foreach ($all_bookings as $booking)
            {
                echo "\"" . $booking['Customer']['last_name'] . " " . $booking['Customer']['first_name'] . " " . $booking['Booking']['alo_booking_code'] . " " . $booking['Booking']['hotel_booking_code'] . "\"" . ($count < $bookings_count ? "," : "");
                $count++;
            }
        }
        ?>
    ];
    $( "#BookingQuery" ).autocomplete({
        source: availableTags,
        select: function (event, ui){
            search(ui.item.value.trim());
        }
    });
});
</script>

<table class="table1">
    <tr>
        <th><?php echo $this->Paginator->sort('Mã booking', 'Booking.alo_booking_code'); ?></th>
        <th><?php echo $this->Paginator->sort('Tên khách', 'Customer.first_name'); ?></th>
        <th><?php echo $this->Paginator->sort('Khách sạn', 'Hotel.name'); ?></th>
        <th><?php echo $this->Paginator->sort('Check in', 'Booking.checkin_date'); ?></th>
        <!-- <th><?php echo $this->Paginator->sort('Check out', 'Booking.checkout_date'); ?></th> -->
        <th><?php echo $this->Paginator->sort('Hạn thu cọc', 'Booking.alo_prepaid_date'); ?></th>
        <th><?php echo $this->Paginator->sort('Hạn thu hết', 'Booking.alo_fullcharge_date'); ?></th>
        <th><?php echo $this->Paginator->sort('Tình trạng', 'BookingStatus.name'); ?></th>
        <th class="gross<?php echo $hide ?>"><?php echo $this->Paginator->sort('Lợi nhuận', 'Booking.profit'); ?></th>
        <th></th>
    </tr>
    <?php foreach ($bookings as $booking): ?>
    <?php
    $tr_class = '';
    if ($booking['BookingStatus']['name'] == '4. Confirmed khách')
    {
        $tr_class = ' class="confirmed"';
    }
    if ($booking['BookingStatus']['name'] == '7. Hủy' || $booking['BookingStatus']['name'] == '8. Kết thúc')
    {
        $tr_class = ' class="done"';
    }
    ?>
    <tr<?php echo $tr_class ?>>
        <td><?php echo $booking['Booking']['hotel_booking_code'] ? $booking['Booking']['hotel_booking_code'] : $booking['Booking']['alo_booking_code'] ?></td>
        <td><?php echo $booking['Customer']['full_name'] ?></td>
        <td><?php echo $booking['Hotel']['name'] ?></td>
        <td><?php echo date('d/m/y', strtotime($booking['Booking']['checkin_date'])) ?></td>
        <!-- <td><?php echo date('d/m/y', strtotime($booking['Booking']['checkout_date'])) ?></td> -->
        <td><?php echo date('d/m/y', strtotime($booking['Booking']['alo_prepaid_date'])) ?></td>
        <td><?php echo date('d/m/y', strtotime($booking['Booking']['alo_fullcharge_date'])) ?></td>
        <td><?php echo $booking['BookingStatus']['name'] ?></td>
        <td class="gross<?php echo $hide ?>"><?php echo number_format($booking['Booking']['profit'], 1, '.', ',') ?></td>
        <td>
        <?php echo $this->Html->link('Xem', array('controller' => 'bookings', 'action' => 'show', $booking['Booking']['id'])); ?> |
        <?php echo $this->Html->link('Sửa', array('controller' => 'bookings', 'action' => 'edit', $booking['Booking']['id'])); ?>
        </td>
    </tr>
    <?php endforeach; ?>
</table>

<?php if (empty($hide)): ?>
<p class="<?php echo $hide ?>"><strong>Tổng lợi nhuận:</strong> <?php echo number_format($total_profit, 1, '.', ',') ?></p>
<?php endif; ?>

<?php echo $this->element('paginator'); ?>